package com.epalmpay.controller.apiShopApp;

import com.alibaba.fastjson.JSON;
import com.epalmpay.commom.BaseController;
import com.epalmpay.dto.posapi.PosApiRequestDTO;
import com.epalmpay.dto.posapi.PosApiResponseDTO;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.service.app.IAppShopCollectionService;
import com.epalmpay.service.app.IAppShopService;
import com.epalmpay.service.posapi.PosApiService;
import com.epalmpay.util.Response;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *  收款
 *
 * Created by Administrator on 2018/2/26.
 */
@Controller
@RequestMapping(value="/app/shop/collection")
public class ShopCollectionController extends BaseController{

    @Resource
    private IAppShopCollectionService appShopCollectionService;
    @Resource
    private PosApiService posApiService;

    @RequestMapping(value="/isHas",method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Response isHas(String shopId){
        logBefore(logger, "首页");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
//        Boolean flag = tokenService.checkToken(shopId, token);
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }
        try {
            Response response = appShopCollectionService.isHas(shopId);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    /**
     *  去收款 判断时候是会员跳不同的页面
     * @param phone
     * @param shopId
     * @return
     */
    @RequestMapping(value="/memberData",method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Response memberData(String phone,String shopId,String tradeMoney){
        logBefore(logger, "去收款");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("会员手机号", phone);
        resultMap.put("交易金额", tradeMoney);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
//        Boolean flag = tokenService.checkToken(shopId, token);
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }
        try {
            Response response = appShopCollectionService.memberData(phone,shopId,tradeMoney);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    /**
     * 收款码
     * @param shopId
     * @return
     */
    @RequestMapping(value="/collectImg",method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Response collectImg(String shopId){
        logBefore(logger, "收款二维码");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
//        Boolean flag = tokenService.checkToken(shopId, token);
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }
        try {
            Response response = appShopCollectionService.collectImg(shopId);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }


    /**
     * 收款账户的信息
     * @param shopId
     * @return
     */
    @RequestMapping(value="/shopCollectionInfo",method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Response shopCollectionInfo(String shopId){
        logBefore(logger, "收款二维码");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
//        Boolean flag = tokenService.checkToken(shopId, token);
//        if (!flag) {
//            return Response.getError(Response.LOGIN_CODE,"登录失效");
//        }
        try {
            Response response = appShopCollectionService.shopCollectionInfo(shopId);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    @RequestMapping(value="/collectionRecord",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response collectionRecord(String shopId,String joinTime,String endTime,Integer collectType){
        logBefore(logger, "商户的交易记录");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.collectionRecord(shopId,joinTime,endTime,collectType);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }


    }







    /**
     * 商户的交易记录
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/tradeList", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Response tradeList(String shopId, String startDate,String endDate, Integer payType,Integer transactionType, Integer pageNo, Integer pageSize) throws Exception {
        logBefore(logger, "商户的交易记录");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("起始页", pageNo+"");
        resultMap.put("每页条数", pageSize+"");
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.tradeList(shopId,startDate,endDate,payType,transactionType,pageNo,pageSize);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }
    /**
     * 商城的交易记录
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/entityGoodTradeList", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Response entityGoodTradeList(String shopId, String startDate,String endDate, Integer payType,Integer transactionType, Integer pageNo, Integer pageSize) throws Exception {
        logBefore(logger, "商户的交易记录");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("起始页", pageNo+"");
        resultMap.put("每页条数", pageSize+"");
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.entityGoodTradeList(shopId,startDate,endDate,payType,transactionType,pageNo,pageSize);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }






    /**
     * 商户的交易记录详情
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/tradeDetail", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public Response tradeDetail(String tradeId,String shopId,String memberId) throws Exception {
        logBefore(logger, "商户的交易记录详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", tradeId);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.tradeDetail(tradeId,shopId,memberId);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    /**
     * 店铺交易统计报表
     * @param shopId
     * @param statisDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/shopTradeStatisData",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response shopTradeStatisData(String shopId,String statisDate)throws Exception{
        logBefore(logger, "店铺交易统计报表");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.shopTradeStatisData(shopId,statisDate);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    /**
     * 店铺交易统计报表 收款笔数详情
     * @param shopId
     * @param statisDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/collectionDetail",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response collectionDetail(String shopId,String statisDate)throws Exception{
        logBefore(logger, "收款笔数详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("统计日期", statisDate);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.collectionDetail(shopId,statisDate);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    /**
     * 店铺交易统计报表 消费会员人数详情
     * @param shopId
     * @param statisDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/customerMemberDetail",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response customerMemberDetail(String shopId,String statisDate)throws Exception{
        logBefore(logger, "消费会员人数详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("统计日期", statisDate);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.customerMemberDetail(shopId,statisDate);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    /**
     * 店铺交易统计报表 优惠券数据详情
     * @param shopId
     * @param statisDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/couponDataDetail",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response couponDataDetail(String shopId,String statisDate,Integer pageNo,Integer pageSize)throws Exception{
        logBefore(logger, "优惠券数据详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("统计日期", statisDate);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.couponDataDetail(shopId,statisDate,pageNo,pageSize);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }


    /**
     * 店铺交易统计报表 积分数据详情
     * @param shopId
     * @param statisDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/bonusDataDetail",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response bonusDataDetail(String shopId,String statisDate,Integer pageNo,Integer pageSize)throws Exception{
        logBefore(logger, "积分数据详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("统计日期", statisDate);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.bonusDataDetail(shopId,statisDate,pageNo,pageSize);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    /**
     * 店铺交易统计报表 会员新增详情
     * @param shopId
     * @param statisDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/addMemberDataDetail",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response addMemberDataDetail(String shopId,String statisDate,Integer pageNo,Integer pageSize)throws Exception{
        logBefore(logger, "会员新增详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("统计日期", statisDate);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.addMemberDataDetail(shopId,statisDate,pageNo,pageSize);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }


    /**
     * 店铺交易统计报表 券码核销详情
     * @param shopId
     * @param statisDate
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/couponCodeVerifyDetail",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response couponCodeVerifyDetail(String shopId,String statisDate)throws Exception{
        logBefore(logger, "会员新增详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("统计日期", statisDate);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.couponCodeVerifyDetail(shopId,statisDate);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    @RequestMapping(value = "/tranUpload", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public PosApiResponseDTO tranUpload(@RequestBody PosApiRequestDTO posApiRequestDTO) throws Exception {
        PosApiResponseDTO posApiResponseDTO = new PosApiResponseDTO();
        posApiResponseDTO.setCode("400");
        posApiResponseDTO.setMsg("请求非法");


        Map<String, Object> params = posApiRequestDTO.getParams();
        if(params == null) {
            return posApiResponseDTO;
        }
//        if(cmd == null || params == null) {
//            return posApiResponseDTO;
//        }

        Map<String, Object>  result = posApiService.tranUpload(params);

        if(result != null) {
            if((Integer)result.get("responseCode") == 1) {
                result.remove("responseCode");
                posApiResponseDTO.setResult(result);
                logger.debug(JSON.toJSONString(result));
                posApiResponseDTO.setCode("A00006");
                posApiResponseDTO.setMsg("成功");
            } else {
                posApiResponseDTO.setCode(result.get("responseCode").toString());
                posApiResponseDTO.setMsg((String) result.get("responseMsg"));
            }
        }

        return posApiResponseDTO;
    }



    @RequestMapping(value = "/appCouponInfo", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response appCouponInfo(String sid,String couponCode) throws Exception {
        logBefore(logger, "会员新增详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", sid);
        resultMap.put("券码", couponCode);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.appCouponInfo(sid,couponCode);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }

    }



    @RequestMapping(value = "/appCouponVerify", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response appCouponVerify(String sid,String couponCode) throws Exception {
        logBefore(logger, "会员新增详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", sid);
        resultMap.put("券码", couponCode);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.appCouponVerify(sid,couponCode);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }

    }
    @RequestMapping(value = "/appCouponRefund", method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response appCouponRefund(String sid,String couponCode) throws Exception {
        logBefore(logger, "会员新增详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", sid);
        resultMap.put("券码", couponCode);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.appCouponRefund(sid,couponCode);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }

    }


    /**
     * 验券记录
     * @param shopId
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/testCouponList",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response testCouponList(String shopId,String startDate,String endDate,Integer pageNo,Integer pageSize)throws Exception{
        logBefore(logger, "验券记录");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.testCouponList(shopId,startDate,endDate,pageNo,pageSize);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }

    /**
     * 验券详情
     * @param id
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/testCouponDetail",method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public Response testCouponDetail(String id,String shopId,String memberId)throws Exception{
        logBefore(logger, "验券详情");
        HashMap<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("商户编号", shopId);
        resultMap.put("订单编号", id);
        resultMap.put("会员编号", memberId);
        String nullParam = Response.checkNull(resultMap);
        if (null != nullParam) {
            return Response.getError(nullParam + "不能为空");
        }
        try {
            Response response = appShopCollectionService.testCouponDetail(id,shopId,memberId);
            return response;
        } catch (ApplicationException ae) {
            logger.error(ae);
            ae.printStackTrace();
            return Response.getError(ae.errMsg);
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return Response.getError("服务器异常");
        }
    }


}
